﻿using System;
using System.Collections.Generic;
using Web7.Core.InversionOfControl;
using Web7.Core.Configuration;
using Web7.Ioc;
///using Web7.Ioc;

namespace Web7.Core.BackgroundServices
{
    public class BackgroundServiceRegistry : IBackgroundServiceRegistry
    {
        private readonly IDependencyInjector _dependencyInjector;
        private readonly List<IBackgroundServiceExecutor> _executors;

        public BackgroundServiceRegistry(IDependencyInjector dependencyInjector)
        {
            this._dependencyInjector = dependencyInjector;
            _executors = new List<IBackgroundServiceExecutor>();
        }

        #region IBackgroundServiceRegistry Members

        public void Clear()
        {
            _executors.Clear();
        }

        public void Add<TExecutor, TService>(Web7AppConfigurationElement appConfiguration, string name, TimeSpan defaultInterval)
            where TExecutor : class, IBackgroundServiceExecutor
            where TService : IBackgroundService
        {
            _executors.Add((TExecutor)Activator.CreateInstance(typeof(TExecutor), _dependencyInjector, typeof(TService), appConfiguration));
        }

        public IEnumerable<IBackgroundServiceExecutor> GetBackgroundServices()
        {
            return _executors;
        }

        #endregion
    }
}
